package de.baumann.browser.present

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.QiNiuToken
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IUserInfoView
import de.baumann.browser.model.UserModel
import de.baumann.browser.rx.RxSchedulers
import de.baumann.browser.utils.Constants
import de.baumann.browser.utils.getUser
import de.baumann.browser.utils.updateHead

/**
 *@author:
 *@date:2019/10/24 17:17
 *@discription: 个人信息
 */
class UserInfoPresenter : BasePresenter<IUserInfoView>() {
    private val userModel: UserModel = UserModel()
    fun getUserInfo() {
        val loginInfo = getUser()
        if (loginInfo != null) {
            if (loginInfo.headUrl != null) {
                view.setHeadhUrl(loginInfo.headUrl)
            }
            view.nickname = loginInfo.user_name
            view.setPhone(loginInfo.phone)
            view.qq = loginInfo.qq
            view.setPoolName(loginInfo.poolname)
            view.ethUrl = loginInfo.eth_address
        }
    }

    fun logout() {
        this.userModel.logout()
                .subscribe(object : OdinObserver<Any>(view) {
                    override fun onSuccess(data: Any?) {
                        if (view!=null){
                            view.logoutSuccess()
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        if (view!=null){
                            view.logoutSuccess()
                        }
                    }
                })
    }

    fun getQiNiuToken() {
        this.userModel.qiNiuToken
                .subscribe(object : OdinObserver<QiNiuToken>(view) {
                    override fun onSuccess(data: QiNiuToken?) {
                        if (view != null && data != null)
                            view.setToken(data.ossToken)
                    }
                })
    }

    fun editHead(headUrl: String) {
        this.userModel.editHead(Constants.QN + headUrl)
                .subscribe(object : OdinObserver<Any>(view) {
                    override fun onSuccess(data: Any?) {
                        updateHead(Constants.QN + headUrl)
                    }
                })
    }

}